package ca.nfb.media.nfbPlayer.ui
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.ui.Mouse;

	public class Scroller extends MovieClip
	{
		private var text			:TextField;
		private var scrollSpeed		:Number = 2;
		private var scrollHeight 	:Number;
		private var scrollRecta		:Rectangle;
		private var scrollPoint		:Point;
		
		
		public function Scroller(text_txt:TextField)
		{
			super();
			text = text_txt;
			this.addEventListener(Event.ADDED_TO_STAGE, added, false, 0, true);
		}
		
		private function added(evt:Event):void
		{
			
			scrollHeight 						= this.scrollTrack_mc.height - thumb_mc.height;
			
			MovieClip(thumb_mc).mouseEnabled	= true;
			MovieClip(thumb_mc).useHandCursor 	= true;
			MovieClip(thumb_mc).buttonMode		= true;
			
			scrollRecta = new Rectangle(0, 0, 0, scrollHeight);
			reset();
		}
		
		
		public function checkTextSize():void
		{
			trace("numlines="+text.numLines);
			trace("maxScrollV="+text.maxScrollV);
			if (text.maxScrollV > 1)
				this.visible = true;
			else
				this.visible = false;		 
		}
		
		private function initMouseEvents():void
		{
			clearMouseEvents();
			MovieClip(thumb_mc).addEventListener(MouseEvent.MOUSE_DOWN, mDown, false, 0, true);
			MovieClip(thumb_mc).addEventListener(MouseEvent.MOUSE_UP, mUp, false, 0, true);
			MovieClip(scrollTrack_mc).addEventListener(MouseEvent.CLICK, trackClicked, false, 0, true);
			
		}
		
		private function trackClicked(evt:MouseEvent):void
		{
			var mY:Number = this.mouseY;
			if (mY > scrollRecta.bottom)
			{
				mY = scrollRecta.bottom;
			}
			else if (mY < 0)
			{
				mY = 0;
			}	
			
			scrollPoint = new Point(0, mY);	
			MovieClip(thumb_mc).y = scrollPoint.y;
			checkScrollPoint();
		}
		
		private function mDown(evt:MouseEvent):void
		{
			MovieClip(thumb_mc).startDrag(false, scrollRecta);
			MovieClip(thumb_mc).addEventListener(MouseEvent.MOUSE_MOVE, mMoved, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, mUp, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mMoved, false, 0, true);
		}
		
		
		private function mUp(evt:MouseEvent):void
		{
			MovieClip(thumb_mc).stopDrag();
			if (MovieClip(thumb_mc).hasEventListener(MouseEvent.MOUSE_MOVE))
				MovieClip(thumb_mc).removeEventListener(MouseEvent.MOUSE_MOVE, mMoved);
				
			if (stage.hasEventListener(MouseEvent.MOUSE_UP))
				stage.removeEventListener(MouseEvent.MOUSE_UP, mUp);	
				
			if (stage.hasEventListener(MouseEvent.MOUSE_MOVE))
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, mMoved);		
			
		}
		
		private function clearMouseEvents():void
		{
			if (MovieClip(thumb_mc).hasEventListener(MouseEvent.MOUSE_MOVE))
				MovieClip(thumb_mc).removeEventListener(MouseEvent.MOUSE_MOVE, mMoved);
				
			if (MovieClip(thumb_mc).hasEventListener(MouseEvent.MOUSE_DOWN))	
				MovieClip(thumb_mc).removeEventListener(MouseEvent.MOUSE_DOWN, mDown);
				
			if (MovieClip(thumb_mc).hasEventListener(MouseEvent.MOUSE_UP))
				MovieClip(thumb_mc).removeEventListener(MouseEvent.MOUSE_UP, mUp);
				
			if (MovieClip(scrollTrack_mc).hasEventListener(MouseEvent.CLICK))
				MovieClip(scrollTrack_mc).removeEventListener(MouseEvent.CLICK, trackClicked);	
				
			if (stage.hasEventListener(MouseEvent.MOUSE_UP))
				stage.removeEventListener(MouseEvent.MOUSE_UP, mUp);	
				
			if (stage.hasEventListener(MouseEvent.MOUSE_MOVE))
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, mMoved);		
			
		}
		
		private function mMoved(evt:MouseEvent):void
		{
			scrollPoint = new Point(thumb_mc.x, thumb_mc.y);
			checkScrollPoint();
			evt.updateAfterEvent();
		}
		
		private function checkScrollPoint():void
		{
			var scrollFactor:Number = scrollPoint.y / scrollHeight;
			text.scrollV = scrollFactor * text.maxScrollV+1;
		}
		
		public function reset():void
		{
			initMouseEvents();
			MovieClip(thumb_mc).y = 0;
			checkTextSize();

		}
		
	}
}